package com.mc.mcgoweb.utils;


import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;

public class HttpRequestUtil {

	 private static CloseableHttpClient httpClient;
	    static {
	 
	        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
	        cm.setMaxTotal(100);
	        cm.setDefaultMaxPerRoute(20);
	        cm.setDefaultMaxPerRoute(50);
	        httpClient = HttpClients.custom().setConnectionManager(cm).build();
	    }
	 
	    public static String get(String url) {
	        CloseableHttpResponse response = null;
	        BufferedReader in = null;
	        String result = "";
	        try {
	            HttpGet httpGet = new HttpGet(url);
	            RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(30000).setConnectionRequestTimeout(30000).setSocketTimeout(30000).build();
	            httpGet.setConfig(requestConfig);
	            httpGet.setConfig(requestConfig);
	            httpGet.addHeader("Content-type", "application/json; charset=utf-8");
	            httpGet.setHeader("Accept", "application/json");
	            httpGet.setHeader("Cookie","JSESSIONID=32CE8630F742D58D0866B4BF586B0CAC; __jsluid=42b2383c22dc90394ec08ba02e8abe3d; _ga=GA1.2.912227588.1537515723; Hm_lvt_2fed596a8579837ff549be6fc4993ab3=1551194428; Hm_lvt_4648e311666e885b88bd176fdddc403c=1551194428; VCODE=705793cc62484e26b1d09720974a5928; CASTGC=875ea5ad14fe4b9c9cac78a0c3030f2c; staging=0; CompCode=yimidida; DeptCode=yimidida; TheProvinceCode=null; Hm_lvt_ecf143ba8cef983e65a6fa37f9989911=1553598003,1553674219,1553756422,1553791363; Hm_lpvt_ecf143ba8cef983e65a6fa37f9989911=1553791441; JSESSIONID=32CE8630F742D58D0866B4BF586B0CAC; gillion-res-version=201903282339");
	            response = httpClient.execute(httpGet);
	            in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
	            StringBuffer sb = new StringBuffer("");
	            String line = "";
	            String NL = System.getProperty("line.separator");
	            while ((line = in.readLine()) != null) {
	                sb.append(line + NL);
	            }
	            in.close();
	            result = sb.toString();
	        } catch (IOException e) {
	            e.printStackTrace();
	        } finally {
	            try {
	                if (null != response) {
	                    response.close();
	                }
	            } catch (IOException e) {
	                e.printStackTrace();
	            }
	        }
	        return result;
	    }
	 
	    public static String post(String url, String jsonString) {
	        CloseableHttpResponse response = null;
	        BufferedReader in = null;
	        String result = "";
	        try {
	            HttpPost httpPost = new HttpPost(url);
	            RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(30000).setConnectionRequestTimeout(30000).setSocketTimeout(30000).build();
	            httpPost.setConfig(requestConfig);
	            httpPost.setConfig(requestConfig);
	            httpPost.addHeader("Content-type", "application/json; charset=utf-8");
	            httpPost.setHeader("Accept", "application/json");
	            httpPost.setEntity(new StringEntity(jsonString, Charset.forName("UTF-8")));
	            response = httpClient.execute(httpPost);
	            in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
	            StringBuffer sb = new StringBuffer("");
	            String line = "";
	            String NL = System.getProperty("line.separator");
	            while ((line = in.readLine()) != null) {
	                sb.append(line + NL);
	            }
	            in.close();
	            result = sb.toString();
	        } catch (IOException e) {
	            e.printStackTrace();
	        } finally {
	            try {
	                if (null != response) {
	                    response.close();
	                }
	            } catch (IOException e) {
	                e.printStackTrace();
	            }
	        }
	        return result;
	    }

	
}
